﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace wpfTool
{
    /// <summary>
    /// Interaction logic for FileInput.xaml
    /// </summary>
    public partial class FileInput : UserControl, IPropertyGridItem
    {
        int id = 0;

        public string FileName
        {
            get { return theLabel.Text; }
            set { theLabel.Text = value; }
        }

        public FileInput()
        {
            InitializeComponent();
        }

        private void StackPanel_MouseEnter(object sender, MouseEventArgs e)
        {
            theLabel.Visibility = Visibility.Collapsed;
            theButton.Visibility = Visibility.Visible;
        }

        private void StackPanel_MouseLeave(object sender, MouseEventArgs e)
        {
            theLabel.Visibility = Visibility.Visible;
            theButton.Visibility = Visibility.Collapsed;
        }

        private void theButton_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog d = new Microsoft.Win32.OpenFileDialog();
            if (d.ShowDialog() == true) // Result could be true, false or null
            {
                this.FileName = d.FileName;
                FileNameChangedEvent(this, FileName);
            }
        }

        public event Window1.PropertyGridItemChangeHandler FileNameChangedEvent;

        void IPropertyGridItem.registerItemUpdateEvent(wpfTool.PropertyGrid grid)
        {
            FileNameChangedEvent += new Window1.PropertyGridItemChangeHandler(grid.OnGridItemChanged);
        }

        void IPropertyGridItem.unregisterItemUpdateEvent(wpfTool.PropertyGrid grid)
        {
            FileNameChangedEvent -= grid.OnGridItemChanged;
        }

        int IPropertyGridItem.getID()
        {
            return id;
        }

        void IPropertyGridItem.setID(int i)
        {
            if (id != 0)
            {
                MessageBox.Show("ID Exception");
                return;
            }

            id = i;
        }
    }
}
